﻿using System;
using System.Web;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfDealerRegOne : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo oCBizShpCommercialTenantInfo = new AppLogic.DataAlc.CBizShpCommercialTenantInfo())
                {
                    DataSet ds = oCBizShpCommercialTenantInfo.SelectByUserNme(this.oUserInfo.Operator);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (ds.Tables[0].Rows[0]["COMM_TENANT_SID"].ToString() == "0")
                        {
                            Response.Redirect("/c_DealerRegTwo.htm");
                        }
                        else if (ds.Tables[0].Rows[0]["COMM_TENANT_SID"].ToString() == "1" )
                        {
                            Response.Redirect("/c_DealerRegTwoUp.htm");
                        }
                        else if (ds.Tables[0].Rows[0]["COMM_TENANT_SID"].ToString() == "2" || ds.Tables[0].Rows[0]["COMM_TENANT_SID"].ToString() == "3")
                        {
                            Response.Redirect("/c_DealerRegThree.htm");
                        } 
                    }
                }
                BindControls();
            }

        }

        private void BindControls()
        {
            if (!IsPostBack)
            {
                BoundFromProvince();
            }
        }


        private void BoundFromProvince()
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT * FROM BSE_PROVINCE ";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplPROVINCE_ID", ds, "PROVINCE_NME", "PROVINCE_ID", "", "", true);
            }
        }

        private void BoundFromCity(string strFatherId)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT * FROM BSE_PROVINCE_CITY WHERE (FATHER_ID='" + strFatherId + "')";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplCITY_ID", ds, "CITY_NME", "CITY_ID", "", "", true);
            }
        }

        private void BoundArea(string strFatherId)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT * FROM BSE_PROVINCE_CITY_AREA WHERE (FATHER_ID='" + strFatherId + "')";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplAREA_ID", ds, "AREA_NME", "AREA_ID", "", "", true);
            }
        }
         
        public override void DoAction()
        {
            if (this.strAction == "dplPROVINCE_ID_change")
            {
                BoundFromCity(this.GetParamHtValue("PROVINCE_ID"));
            }
            else if (this.strAction == "dplCITY_ID_change")
            {
                BoundArea(this.GetParamHtValue("CITY_ID"));
            } 
            else if (this.strAction == "REGIST_STEP1")
            {
                if (this.GetParamHtValue("VALIDATE_CDE") != (string)GetSession(this.sCurrPageRadom + msBsc.Cls.ValidateCode.VALIDATECODEKEY, "2"))
                {
                    this.ExecJScript("jQuery.getValidateImg('J_CheckCodeContainer');");
                    this.Alert("验证码错误或已过期，请重新录入！");
                    return;
                }
                using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo oCBizShpCommercialTenantInfo = new AppLogic.DataAlc.CBizShpCommercialTenantInfo())
                {
                    DataSet ds = oCBizShpCommercialTenantInfo.SelectByUserNme(this.oUserInfo.Operator);
                    DataRow drnew;
                    if (ds.Tables[0].Rows.Count <= 0)
                    {
                        drnew = ds.Tables[0].NewRow();
                        drnew["COMM_TENANT_ID"] = oCBizShpCommercialTenantInfo.DataObj.NewID();
                        drnew["COMM_TENANT_SID"] = "0";
                        drnew["USER_NME"] = this.oUserInfo.Operator;
                        ds.Tables[0].Rows.Add(drnew);
                    }
                    else
                    {
                        drnew = ds.Tables[0].Rows[0];
                    } 
                    msBsc.AppLogic.Cfg.HttpTools.CopyHashToDataRow(this.htParamPostData, ds.Tables[0].Rows[0]);
                    if (this.GetParamHtValue("COMM_TYPE_CID_1").ToLower() == "true" || this.GetParamHtValue("COMM_TYPE_CID_1").ToLower() == "1")
                    {
                        drnew["COMM_TYPE_CID"] = "1";
                    }
                    if (this.GetParamHtValue("COMM_TYPE_CID_2").ToLower() == "true" || this.GetParamHtValue("COMM_TYPE_CID_2").ToLower() == "1")
                    {
                        drnew["COMM_TYPE_CID"] = "2";
                    }
                    if (drnew["COMM_TYPE_CID"].ToString()==string.Empty)
                    {
                        this.Alert("请选择个人、或企业！");
                        return;
                    }
                    oCBizShpCommercialTenantInfo.Update(ds);
                }





                //if (this.GetParamHtValue("EMAIL") != string.Empty && this.GetParamHtValue("VALIDATE_CDE") != string.Empty)
                //{
                //    string strcontent = "";
                //    using (System.IO.StreamReader sr = new System.IO.StreamReader(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) + "Template\\registmailtmp.htm"))
                //    {
                //        strcontent = sr.ReadToEnd();
                //    }
                //    msBsc.AppLogic.Cfg.DesCry ooo=new AppLogic.Cfg.DesCry();
                //    string strurl = msBsc.AppLogic.Cfg.DesCry.encMe(this.GetParamHtValue("EMAIL") + "," + DateTime.Now.ToString(), "rmdes");
                //    strurl = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.httpUrl + "c_RegUserInfoFill.htm?KEY=" + strurl;

                //    strcontent = strcontent.Replace("{REGISTER_MAIL_USER_NME}", this.GetParamHtValue("EMAIL").Split('@')[0]).Replace("{REGISTER_MAIL_URL}", strurl);
                //    msBsc.SvRef.SendEMail oSendEMail = new SvRef.SendEMail();
                //    oSendEMail.SendEmailTo(this.GetParamHtValue("EMAIL").Split('@')[0], this.GetParamHtValue("EMAIL"), "", strcontent);
                //    //this.ExecJScript("debugger;");
                //this.ExecJScript("LS.set('CURR_REG_MAIL','" + this.GetParamHtValue("EMAIL") + "');");
                //this.ExecJScript("LS.set('" + this.GetParamHtValue("EMAIL") + "','STEP1');");
                //this.ExecJScript("LS.set('" + this.GetParamHtValue("EMAIL") + "_RDT','" + DateTime.Now.ToString() + "');");
                this.ExecJScript("window.location.href = '/c_DealerRegTwo.htm';");

                SetSession(this.sCurrPageRadom + msBsc.Cls.ValidateCode.VALIDATECODEKEY, null, "2");
                //}
            } 
        }

    }
}